{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "odj1Coq5H080"
      },
      "outputs": [],
      "source": [
        "#@title ##### License { display-mode: \"form\" }\n",
        "# Copyright 2019 DeepMind Technologies Ltd. All rights reserved.\n",
        "#\n",
        "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
        "# you may not use this file except in compliance with the License.\n",
        "# You may obtain a copy of the License at\n",
        "#\n",
        "#     http://www.apache.org/licenses/LICENSE-2.0\n",
        "#\n",
        "# Unless required by applicable law or agreed to in writing, software\n",
        "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
        "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
        "# See the License for the specific language governing permissions and\n",
        "# limitations under the License."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "dOOzDGYAZcW3"
      },
      "source": [
        "# OpenSpiel\n",
        "\n",
        "* This Colab gets you started with installing OpenSpiel and its dependencies.\n",
        "* OpenSpiel is a framework for reinforcement learning in games.\n",
        "* The instructions are adapted from [here](https://github.com/deepmind/open_spiel/blob/master/docs/install.md)."
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "XC6kQBzWahEF"
      },
      "source": [
        "## Install dependencies and clone repository"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "btLSDV1SaNeP"
      },
      "source": [
        "Let's first check the Python version, make sure to use a Python 3 runtime."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "9pdMX_Vs9Fcn"
      },
      "outputs": [],
      "source": [
        "!python --version"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "-go8n95la5rw"
      },
      "source": [
        "Clone [open_spiel](https://github.com/deepmind/open_spiel) repository and pull in source dependencies:  [pybind11](https://github.com/pybind/pybind11), [DDS](https://github.com/jblespiau/dds), [abseil](https://github.com/abseil)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "9MALNBoMYVxf"
      },
      "outputs": [],
      "source": [
        "INSTALL_DIR = '/usr/local/open_spiel'"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "EFyY5_UGZU_5"
      },
      "outputs": [],
      "source": [
        "!git config --global advice.detachedHead false\n",
        "!git clone https://github.com/deepmind/open_spiel $INSTALL_DIR\n",
        "!git clone -b 'v2.2.4' --single-branch --depth 1 https://github.com/pybind/pybind11.git $INSTALL_DIR/pybind11\n",
        "!git clone -b 'develop' --single-branch --depth 1 https://github.com/jblespiau/dds.git  $INSTALL_DIR/open_spiel/games/bridge/double_dummy_solver\n",
        "!git clone -b '20200225.1' --single-branch --depth 1 https://github.com/abseil/abseil-cpp.git $INSTALL_DIR/open_spiel/abseil-cpp"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "a3xXzNx8HKur"
      },
      "outputs": [],
      "source": [
        "#@title Optional dependencies: { display-mode: \"both\" }\n",
        "BUILD_WITH_HANABI = False #@param {type:\"boolean\"}\n",
        "BUILD_WITH_ACPC = False  #@param {type:\"boolean\"}\n",
        "if BUILD_WITH_HANABI:\n",
        "  %env BUILD_WITH_HANABI=ON\n",
        "  !git clone -b 'master' --single-branch --depth 15 https://github.com/deepmind/hanabi-learning-environment.git $INSTALL_DIR/open_spiel/games/hanabi/hanabi-learning-environment\n",
        "  !pushd $INSTALL_DIR/open_spiel/games/hanabi/hanabi-learning-environment && git checkout  'b31c973' && popd\n",
        "\n",
        "if BUILD_WITH_ACPC:\n",
        "  %env BUILD_WITH_ACPC=ON\n",
        "  !git clone -b 'master' --single-branch --depth 1  https://github.com/jblespiau/project_acpc_server.git $INSTALL_DIR/open_spiel/games/universal_poker/acpc\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "_IijRaWhbrxI"
      },
      "source": [
        "Installing Python requirements:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "WMQkWaqNUCyB"
      },
      "outputs": [],
      "source": [
        "# we keep some baked-in Colab dependencies:\n",
        "!sed  -e '/IPython/d' -e '/pip/d' -e '/matplotlib/d' $INSTALL_DIR/requirements.txt >> /tmp/requirements.txt\n",
        "!pip3 install -r /tmp/requirements.txt"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "EtIO399jcyaQ"
      },
      "source": [
        "## Build `open_spiel`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "5AFHN81u9XKr"
      },
      "outputs": [],
      "source": [
        "!apt-get install clang-9\n",
        "!mkdir -p $INSTALL_DIR/build\n",
        "%cd $INSTALL_DIR/build\n",
        "!cmake -DPython3_EXECUTABLE=`which python3` -DCMAKE_CXX_COMPILER=`which clang++-9` ../open_spiel\n",
        "!make -j$(nproc)\n",
        "%cd /content"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "w6_vb9WPAuOU"
      },
      "source": [
        "## Set `PYTHONPATH`"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "XdvmS5zrAtae"
      },
      "outputs": [],
      "source": [
        "import sys\n",
        "import os\n",
        "sys.path.append(INSTALL_DIR)\n",
        "sys.path.append(os.path.join(INSTALL_DIR, 'build/python'))  # for pyspiel.so"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "ss85qWHe6qEZ"
      },
      "outputs": [],
      "source": [
        "# verify that Python can find the open_spiel & pyspiel modules\n",
        "import importlib\n",
        "assert importlib.util.find_spec(\"open_spiel\") is not None\n",
        "assert importlib.util.find_spec(\"pyspiel\") is not None"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "cbXshy6wEH9_"
      },
      "source": [
        "## (optional) Run `CMake` tests"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "k8L1Al-HEHWD"
      },
      "outputs": [],
      "source": [
        "# run_python_test calls the python interpreter directly thus setting PYTHONPATH\n",
        "%set_env PYTHONPATH=/env/python:$INSTALL_DIR:$INSTALL_DIR/build/python\n",
        "!pushd $INSTALL_DIR/build && ctest -j$(nproc) --output-on-failure ../open_spiel && popd"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {
        "colab_type": "text",
        "id": "jUtlXZ8FBnAL"
      },
      "source": [
        "# It's play time!"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "colab": {},
        "colab_type": "code",
        "id": "ewMXCaUw8d9Q"
      },
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pyspiel\n",
        "\n",
        "game = pyspiel.load_game(\"tic_tac_toe\")\n",
        "state = game.new_initial_state()\n",
        "\n",
        "while not state.is_terminal():\n",
        "  state.apply_action(np.random.choice(state.legal_actions()))\n",
        "  print(str(state) + '\\n')"
      ]
    }
  ],
  "metadata": {
    "accelerator": "GPU",
    "colab": {
      "collapsed_sections": [],
      "name": "install_open_spiel.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
